
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.TMS.MallInterface.MallOrder;

namespace BCData.TMS.MallInterface.MallOrder
{
    public class MallOrderData : IMallOrderData
    {
        private readonly IDatabaseContext database;
        private readonly string QUERY = @"SELECT
		    A.MallOrderId,
		    A.MallOrderNo,
		    A.MallShopNo,
		    A.CreateTime,
		    A.PayType,
		    A.IsReturnSignBill,
		    A.IsInsuredPrice,
		    A.StartDate,
		    A.EndDate,
		    A.Remark,
		    A.MallOrderState,
		    A.TransportBusinessType,
		    A.UpdateTime,
            A.IsNeedSplit,
            A.AgentBranchesConfigId
        FROM
            MallOrder A";



        #region 构造函数
        public MallOrderData(IDatabaseContext database)
        {
            this.database = database;
        }
        #endregion

        #region InsertAsync
        /// <summary>
        /// 添加新商城订单纪录
        /// </summary>
        /// <param name="mallOrderEntity"></param>
        /// <returns></returns>
        public async Task<MallOrderEntity> InsertAsync(MallOrderEntity entity)
        {
            return await this.database.Master.InsertAsync(entity, database.Transaction);
        }
        #endregion

        #region updateAsync
        /// <summary>
        /// 修改商城订单记录
        /// </summary>
        /// <param name="mallOrderEntity"></param>
        /// <returns></returns>
        public async Task<MallOrderEntity> UpdateAsync(MallOrderEntity entity)
        {
            return await this.database.Master.UpdateAsync(entity, database.Transaction);
        }
        #endregion 


        #region Delete
        /// <summary>
        /// 删除商城订单记录
        /// </summary>
        /// <param name="mallOrderId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(long mallOrderId)
        {
            string sql = "DELETE FROM mallorder WHERE MallOrderId = ?mallOrderId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MallOrderId", mallOrderId, DbType.Int64);
            return await this.database.Master.ExecuteAsync(sql, parameters, database.Transaction) > 0;
        }
        #endregion

        #region GetById
        /// <summary>
        /// 获取商城订单记录
        /// </summary>
        /// <param name="mallOrderId"></param>
        /// <returns></returns>
        public async Task<MallOrderEntity> GetEntityAsync(long mallOrderId)
        {
            string sql = QUERY + " WHERE  A.MallOrderId = ?mallOrderId";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MallOrderId", mallOrderId, DbType.Int64);
            return await this.database.Slave.QueryEntityAsync<MallOrderEntity>(sql, parameters);
        }
        #endregion

        #region GetByNo
        /// <summary>
        /// 获取商城订单记录
        /// </summary>
        /// <param name="mallOrderId"></param>
        /// <returns></returns>
        public async Task<MallOrderEntity> GetEntityAsync(string mallOrderNo)
        {
            string sql = QUERY + " WHERE  A.MallOrderNo = ?mallOrderNo";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MallOrderNo", mallOrderNo, DbType.AnsiString);
            return await this.database.Slave.QueryEntityAsync<MallOrderEntity>(sql, parameters);
        }
        #endregion

        #region GetListAsync 查询商城订单记录
        public async Task<(IEnumerable<MallOrderEntity>, int)> GetListAsync(int pageIndex, int pageSize)
        {

            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();


            string sql = string.Format(@"{0} {1} ; SELECT COUNT(1) FROM  mallorder  {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize
                  );

            return await this.database.Slave.QueryPageableListAsync<MallOrderEntity>(sql, parameters);
        }
        #endregion

    }
}
        //接口
        
        
        
